/**
 * 
 */
package org.vsg.stock.test;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.vsg.common.configration.PropertiesSupport;
import org.vsg.stock.basic.persistence.StockCategoryDao;
import org.vsg.stock.extension.service.StockAnalysisService;
import org.vsg.stock.extension.vo.LinkageCateShownSequenceVO;
import org.vsg.stock.extension.vo.LinkageCateStockVO;


/**
 * @author vison
 *
 */
public class StkCategoryLinkageCycleStockList {

	private final static Logger logger = LoggerFactory.getLogger(StkCategoryLinkageCycleStockList.class);	
	
	static private DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	

	private String encoding;

	
	public void exec(ApplicationContext appContext , Date fromDate ) throws Exception {

		PropertiesSupport proSupport = (PropertiesSupport)appContext.getBean("propertyConfigurer");		
		

		String dir = proSupport.getProperties().getProperty("stock.linkage.dir");
		if (dir == null) {
			//logger.warn("Property 'stock.dailyline.dirk' is not defined! ");
			throw new Exception("Property 'stock.linkage.dir' is not defined!");
		}		
		
		StockAnalysisService stkAnalyService = (StockAnalysisService)appContext.getBean("stockAnalysisService");
		StockCategoryDao stockCategoryDao = (StockCategoryDao)appContext.getBean("stockCategoryDao");
		
		// get the date period 
		int cycle= 20;

		
		List<LinkageCateStockVO> list = stkAnalyService.findCateStockByCategoryDesc(fromDate, cycle, "电子信息");

		StringBuilder sb;
		for (LinkageCateStockVO vo: list) {
			sb = new StringBuilder();
			sb.append(vo.getStockCode() ).append(",");
			sb.append(vo.getRate() ).append(",");
			sb.append(vo.getAssemblyCates() ).append(",");
			sb.append(vo.getAssemblyDates() );
			
			logger.info( sb.toString() );
			
		}
	}
	
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Calendar cale = Calendar.getInstance();
		
		cale.add(Calendar.DATE,-1);

		// --- get the from date and to date ---
		Date fromDate = cale.getTime();

				
		
		ClassLoader clsLoader = StkCategoryLinkageCycleStockList.class.getClassLoader();
		// --- run the main page ---
		// build the read the path , dzh's home path
		String[] configContextFiles = {
				"classpath:/configurationContext.xml",
				"classpath*:/applicationContext.xml",
				"classpath*:/dataAccessContext.xml"
		};
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext(configContextFiles,true);
		
	
		StkCategoryLinkageCycleStockList mainProg = new StkCategoryLinkageCycleStockList();
		
		try {
			mainProg.exec(ctx , fromDate );
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		logger.info("program run ... Ok!");
		



	}

}
